package patrol.mobile.com.mobilepatrol.activity.security.emergency;

import android.content.Intent;
import android.view.View;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.bigkoo.pickerview.TimePickerView;
import com.zhy.http.okhttp.callback.StringCallback;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.Date;
import java.util.HashMap;

import butterknife.BindView;
import butterknife.OnClick;
import okhttp3.Call;
import patrol.mobile.com.mobilepatrol.R;
import patrol.mobile.com.mobilepatrol.activity.BaseActivity;
import patrol.mobile.com.mobilepatrol.api.Urls;
import patrol.mobile.com.mobilepatrol.bean.CheckEntityBean;
import patrol.mobile.com.mobilepatrol.manager.UserManager;
import patrol.mobile.com.mobilepatrol.utils.CommonParamsHelper;
import patrol.mobile.com.mobilepatrol.utils.CommonUtil;
import patrol.mobile.com.mobilepatrol.utils.Constants;
import patrol.mobile.com.mobilepatrol.utils.DateUtil;
import patrol.mobile.com.mobilepatrol.utils.GsonUtil;
import patrol.mobile.com.mobilepatrol.utils.net.HttpCallBack;
import patrol.mobile.com.mobilepatrol.utils.net.HttpSender;
import patrol.mobile.com.mobilepatrol.widget.MyRadioGroup;

import static patrol.mobile.com.mobilepatrol.utils.DateUtil.format1;
import static patrol.mobile.com.mobilepatrol.utils.SPUtils.get;

/**
 * Created by fuzhen.li on 2018/9/18.
 */
public class EmergencyCheckAddActivity extends BaseActivity {


    @BindView(R.id.tv_orgName)
    EditText tvOrgName;
    @BindView(R.id.tv_title)
    EditText tvTitle;
    @BindView(R.id.tv_checkorgId)
    EditText tvCheckorgId;
    @BindView(R.id.rb_safe)
    RadioButton rbSafe;
    @BindView(R.id.rb_ergent)
    RadioButton rbErgent;
    @BindView(R.id.rb_total)
    RadioButton rbTotal;
    @BindView(R.id.rg_check_type)
    MyRadioGroup rgCheckType;
    @BindView(R.id.tv_checkCategory)
    TextView tvCheckCategory;
    @BindView(R.id.rb_mid)
    RadioButton rbMid;
    @BindView(R.id.rb_end)
    RadioButton rbEnd;
    @BindView(R.id.rb_often)
    RadioButton rbOften;
    @BindView(R.id.rg_check_way)
    RadioGroup rgCheckWay;
    @BindView(R.id.tv_checkType)
    TextView tvCheckType;
    @BindView(R.id.tv_checkLeader)
    EditText tvCheckLeader;
    @BindView(R.id.tv_checkPerson)
    EditText tvCheckPerson;
    @BindView(R.id.tv_checkDate)
    TextView tvCheckDate;
    @BindView(R.id.tv_next)
    TextView tvNext;
    private CheckEntityBean checkEntityBean = null;
    private String checkCategory;
    private String checkType;

    @Override
    protected int attachLayoutRes() {
        return R.layout.activity_emergency_check_add;
    }

    @Override
    public void setListener() {
        rgCheckType.setOnCheckedChangeListener(new MyRadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(MyRadioGroup radioGroup, int checkedId) {
                switch (checkedId) {
                    case R.id.rb_safe:
                        checkCategory = rbSafe.getText().toString();

                        break;
                    case R.id.rb_ergent:
                        checkCategory = rbErgent.getText().toString();

                        break;
                    case R.id.rb_total:
                        checkCategory = rbTotal.getText().toString();
                        break;
                }

            }
        });
        rgCheckWay.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup radioGroup, int checkedId) {
                switch (checkedId) {
                    case R.id.rb_mid:
                        checkType = rbMid.getText().toString();
                        break;
                    case R.id.rb_end:
                        checkType = rbEnd.getText().toString();
                        break;
                    case R.id.rb_often:
                        checkType = rbOften.getText().toString();
                        break;
                }

            }
        });

    }

    @Override
    public void initData() {
        super.initData();
    }

    @Override
    public void initTitle() {
        iv_back.setImageResource(R.mipmap.up_white);
        iv_back.setVisibility(View.VISIBLE);
        iv_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                CommonUtil.exitActivityAndBackAnim(EmergencyCheckAddActivity.this, true);
            }
        });
        tv_center_title.setVisibility(View.VISIBLE);
        tv_center_title.setText("综合检查编辑");
        tvCheckorgId.setText(UserManager.getUser((String) get(Constants.current_id, "")).bmname);
        tvCheckPerson.setText(UserManager.getryName());
        tvCheckDate.setText(DateUtil.getCurrentDate(format1));
    }


    private void requestData(String id) {
        HttpSender sender = new HttpSender(this, baseUrl, Urls.IEMERGENCY_CHECK__ENTITY_LIST);
        HashMap<String, String> map = new HashMap<>();
        map.put("id", id);
        sender.requestPost(map, new HttpCallBack(sender) {
            @Override
            public void onError(String code, Call call, Exception e) {

            }

            @Override
            public void onResponse(String code, String response) {
                checkEntityBean = GsonUtil.GsonToBean(response, CheckEntityBean.class);
                tvOrgName.setText(checkEntityBean.getOrg().getOrgName());
                tvTitle.setText(checkEntityBean.getTitle());
                tvCheckorgId.setText(checkEntityBean.getCheckorgId().getOrgName());
                tvCheckDate.setText(checkEntityBean.getCheckDate());
            }
        });
    }

    private void submit() {
        final HttpSender sender = new HttpSender(this, baseUrl, Urls.EMERGENCY_SAVE_CHECK_UPLOAD);

        HashMap<String, String> idDateBmnameMap = CommonParamsHelper.getIdDateBmnameMap();

        HashMap<String, Object> map = new HashMap<>();
        map.putAll(idDateBmnameMap);
        map.put("orgName", tvOrgName.getText().toString());
        map.put("checkLeader", tvCheckLeader.getText().toString());
        map.put("checkDate", tvCheckDate.getText().toString());
        map.put("title", tvTitle.getText().toString());
        map.put("checkOrgName", tvCheckorgId.getText().toString());
        map.put("checkCategory", checkCategory);
        map.put("checkType", checkType);
        map.put("checkPerson", tvCheckPerson.getText().toString());


        HashMap<String, Object> map2 = new HashMap<>();
        map2.put("abkWorksCheck", GsonUtil.map2Json(map));


        sender.postJson(GsonUtil.map2Json(map2), new StringCallback() {
            @Override
            public void onError(Call call, Exception e, int id) {
                sender.dismissDialog();
            }

            @Override
            public void onResponse(String response, int id) {
                JSONObject jsonObject = null;
                try {
                    jsonObject = new JSONObject(response);
                    int state = jsonObject.getInt("state");
                    if (200 == state) {
                        sender.dismissDialog();
                        CommonUtil.exitActivityAndBackAnim(EmergencyCheckAddActivity.this, true);
                    } else {
                        Toast.makeText(EmergencyCheckAddActivity.this, "上传失败", Toast.LENGTH_SHORT).show();
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }

            }
        });
    }

    @OnClick({R.id.tv_checkDate, R.id.tv_next})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_checkDate:

                //时间选择器
                TimePickerView pvTime = new TimePickerView.Builder(this, new TimePickerView.OnTimeSelectListener() {
                    @Override
                    public void onTimeSelect(Date date, View v) {
                        tvCheckDate.setText(DateUtil.convert2String(date, format1));
                    }
                }).setType(new boolean[]{true, true, true, true, true, true}).setLabel("年", "月", "日", "时", "分", "秒").build();
                pvTime.show();

                break;
            case R.id.tv_next:
                submit();
                break;
        }
    }
}
